<template>
    <list>
    <header>
    <div><title-bar @back="titleBarBack" :title="titleText"/></div>
    </header>
       <cell v-for="(name,index) in dataList" :key="name">
            <div @click="callGlobalTestEvent(index)" class="cell-item">
                <text style="font-size:30px" class="text">{{name}}</text>
            </div>
       </cell>
    </list>
</template>
<script>
const globalEvent = weex.requireModule("globalEvent");
const test = weex.requireModule("test");
import TitleBar from "../components/TitleBar";
import { toast, alert } from "../utils/config";
export default {
  components: {
    TitleBar
  },
  created: function() {
    let self = this;
    globalEvent.addEventListener("WXApplicationDidBecomeActiveEvent", function(
      e
    ) {
      var param = { message: "WXApplicationDidBecomeActiveEvent" };
      alert(param);
    });
    globalEvent.addEventListener("WXApplicationWillResignActiveEvent", function(
      e
    ) {
      var param = { message: "WXApplicationWillResignActiveEvent" };
      alert(param);
    });
    globalEvent.addEventListener("geolocation", function(e) {
      toast(e.key);
    });
  },
  data: {
    titleText: "GlobalEvent全局事件",
    dataList: [
      "应用被前台的时候触发",
      "应用即将被后台时候触发",
      "点击测试监听全局事件-geolocation",
      "点击移除监听全局事件-geolocation"
    ]
  },
  methods: {
    callGlobalTestEvent: function(index) {
      switch (index) {
        case 2:
          test.testGlobal("test");
          break;
        case 3:
          globalEvent.removeEventListener("geolocation", "1");
          toast("geolocation事件移除成功");
        default:
          break;
      }
    },
    titleBarBack: function() {
      globalEvent.removeEventListener("WXApplicationDidBecomeActiveEvent");
      globalEvent.removeEventListener("WXApplicationWillResignActiveEvent");
      globalEvent.removeEventListener("geolocation");
    }
  },
  beforeDestroy() {
    console.log("beforeDestroy");
  },
  destroyed() {
    console.log("destroyed");
  }
};
</script>
<style scoped src="../components/test.css">
</style>


